﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.CodeMaker.Template
{
    public static class CustomORMTemplate
    {
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="entityNS"></param>
        /// <param name="ormNS"></param>
        /// <param name="baseSql"></param>
        /// <param name="entityName"></param>
        /// <param name="tableSql"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string ToCustomORMTemplate(string entityNS, string ormNS,string baseSql,string entityName,
            string tableSql,string fields)
        {
            var sb = new StringBuilder();

            sb.AppendLine("using System;")
              .AppendLine("using System.Collections.Generic;")
              .AppendLine("using GFluentCode.ORM;")
              .AppendLine("using GFluentCode.Tools;")
              .AppendLine($"using {entityNS};")
              .AppendLine()
              .AppendLine($"namespace  {ormNS}")
              .AppendLine($"{{")
              .AppendLine($"    /// <summary>")
              .AppendLine($"    /// {entityName} ORM")
              .AppendLine($"    /// </summary>")
              .AppendLine($"    public class {entityName}ORM:ORMBase")
              .AppendLine($"    {{")
              .AppendLine($"")
              .AppendLine($"        private string _customSql=@\"{baseSql}\";")
              .AppendLine($"")
              .AppendLine($"        public {entityName}ORM():base() {{ }}")
              .AppendLine()
              .AppendLine($"        private string FormatSql(string where)")
              .AppendLine($"        {{")
              .AppendLine($"            if(where.GIsEmpty()) return _customSql.GToFormat(string.Empty);")
              .AppendLine($"")
              .AppendLine($"            if(where.Trim().StartsWith(\"And\")")
              .AppendLine($"                || where.Trim().StartsWith(\"and\"))")
              .AppendLine($"                return _customSql.GToFormat(where);")
              .AppendLine($"")
              .AppendLine($"            return  _customSql.GToFormat(\" And \" + where);")
              .AppendLine($"        }}")
              .AppendLine()
              .AppendLine($"        /// <summary>")
              .AppendLine($"        /// 判断存在")
              .AppendLine($"        /// </summary>")
              .AppendLine($"        public bool Exists(string where)")
              .AppendLine($"        {{")
              .AppendLine($"            return ExistsBySql<{entityName}>(FormatSql(where));")
              .AppendLine($"        }}")
              .AppendLine($"")
              .AppendLine($"        /// <summary>")
              .AppendLine($"        /// 查询单个实体")
              .AppendLine($"        /// </summary>")
              .AppendLine($"        public {entityName} QuerySingle(string where)")
              .AppendLine($"        {{")
              .AppendLine($"            return QuerySingleBySql<{entityName}>(FormatSql(where));")
              .AppendLine($"        }}")
              .AppendLine($"")
              .AppendLine($"        /// <summary>")
              .AppendLine($"        /// 按条件查询")
              .AppendLine($"        /// </summary>")
              .AppendLine($"        public IList<{entityName}> Query(string where)")
              .AppendLine($"        {{")
              .AppendLine($"            return QueryBySql<{entityName}>(FormatSql(where));")
              .AppendLine($"        }}")
              .AppendLine($"")
              .AppendLine($"        /// <summary>")
              .AppendLine($"        /// 分页查询")
              .AppendLine($"        /// </summary>")
              .AppendLine($"        public IList<{entityName}> QueryPage(string where,string order,int page,int size)")
              .AppendLine($"        {{")
              .AppendLine($"            return QueryPage<{entityName}>(@\"{tableSql}\",\"{fields}\",where,order,page,size);")
              .AppendLine($"        }}")
              .AppendLine($"    }}")
              .AppendLine($"}}");

            return sb.ToString();
        }
    }
}
